
function base58(i)
{
    var lookup = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    var s = "";
    while (i > 0) {
	var r = i % 58;
	s = lookup.charAt(r) + s;
	i = (i - r) / 58;
    }
    return s;
}

function flickrTiny(i)
{
    return "http://flic.kr/p/" + base58(i);
}

function redact(expr)
{
    var iterator = document.evaluate(
	expr,
	document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );

    var toModify = Array();
    try {
	var thisNode = iterator.iterateNext();
	while (thisNode) {
	    toModify.push(thisNode);
	    thisNode = iterator.iterateNext();
	}
    } catch (e) {
    }

    var nrabbits = rabbits.length;
    for (var i = 0; i < toModify.length; ++i) {
	var node = toModify[i];
	/* pick a rabbit */
	var ri = Math.floor(Math.random() * (nrabbits + 1));
	var r = rabbits[ri];
	/* farm server id secret ownername title owner */
	/* clear existing content */
	node.innerHTML = '';
	function makeLink(to) {
	    var anchor = document.createElement("a");
	    var href = document.createAttribute("href");
	    href.nodeValue = to;
	    anchor.setAttributeNode(href);
	    return anchor;
	}
	/* create a link to the original image */
	var imgLink = makeLink(flickrTiny(r[2]));
	/* create img tag */
	var img = document.createElement("img");
	var src = document.createAttribute("src");
	src.nodeValue = "http://farm" + r[0] + ".static.flickr.com/" + r[1] + "/" + r[2] + "_" + r[3] + "_m.jpg";
	img.setAttributeNode(src);
	/* append the image */
	imgLink.appendChild(img);
	node.appendChild(imgLink);
	/* create a div for our attribution stuff */
	var attribution = document.createElement("div");
	attribution.innerHTML = '<a href="http://creativecommons.org/licenses/by-nc-sa/2.0/"><img src="http://i.creativecommons.org/l/by-sa/2.0/80x15.png" alt="Creative Commons ShareAlike" /></a> : ';
	/* create title */
	var title = document.createElement("em");
	title.appendChild(document.createTextNode(r[5]));	
	attribution.appendChild(title);
	attribution.appendChild(document.createTextNode(" by "));
	/* create a link to the author */
	var authorLink = makeLink("http://www.flickr.com/photos/" + r[6]);
	authorLink.appendChild(document.createTextNode(r[4]));
	attribution.appendChild(authorLink);
	node.appendChild(attribution);
    }
}

if (window.location.hostname == "blogs.news.com.au") {
    redact('//div[@class="blog-ee-comment"]');
    redact('//div[@class="threadcomment"]');
} else if (window.location.hostname == "www.heraldsun.com.au") {
    redact('//div[@class="comment-block"]');
} else if (window.location.hostname == "www.news.com.au") {
    redact('//div[@class="comment-block"]');
} else if (window.location.hostname == "www.abc.net.au") {
    redact('//li/p[@class="comment"]')
} else if (window.location.hostname == "www.youtube.com" || window.location.hostname == "youtube.com") {
    redact('//div[@class="comment-text"]');
}
